<?php
class top extends spController
{
	//重定义构造函数
    function __construct(){
    	
        parent::__construct();
        //除登陆页验证session
        $c = $this->spArgs('c','','get');
        $a = $this->spArgs('a','','get');
	
        if($c!='main'||!in_array($a,array('login','login_verify'))){
        	if(!$this->check_session()){$this->jump(spUrl('main', 'login'));}
        }
		//语言
		$this->setLang("cn");
		//面包屑导航
		$this->breadcrumb = array('0'=>array('name'=>'首页','url'=>spUrl('main')));
		//当前域名
		$this->url = trim(dirname($GLOBALS['G_SP']['url']["url_path_base"]),"\/\\");
		
		//站点信息
		$setting = spClass("m_setting");	
		$setting_result = $setting->findAll();

		$this->cms_title = $setting_result[0]['content'];
		$this->cms_description = $setting_result[1]['content'];
		$this->cms_address = $setting_result[2]['content'];
		$this->cms_postcode = $setting_result[3]['content'];
		$this->cms_phone = $setting_result[4]['content'];
		$this->cms_email = $setting_result[5]['content'];
		$this->cms_record = $setting_result[6]['content'];
		$this->cms_ann = $setting_result[7]['content'];
		$this->cms_is_time_visible = $setting_result[8]['content'];

		$this->email_server_address = $setting_result[9]['content'];
		$this->email_server_username = $setting_result[10]['content'];
		$this->email_server_password = $setting_result[11]['content'];
		$this->email_recieve_address = $setting_result[12]['content'];
		$this->web_shouye_info = $setting_result[13]['content'];

		//内容更新导航
		$cat = spClass("m_category");	
		$cat->linker['cat_url_first']['enabled'] = true;
		$this->nav_result = $cat->spLinker()->findAll(array('father_id'=>"2"),'weight DESC');
		$this->content_only = $this->spArgs('content_only','','get');
    }

    	
    //构造左侧导航
    function __left_nav($cat_id=0){
    	if(!is_numeric($cat_id)) $this->__yt_error();
		
		$module = $this->spArgs('c');
		$category = spClass("m_category");
		//查找儿子分类
		$son_info = $category->find(array('module'=>$module,'father_id'=>$cat_id),'weight DESC');
		//查找当前分类
		$cat_info = $category->find(array('module'=>$module,'id'=>$cat_id));
		//查找父分类
		$father_info = $category->find(array('module'=>$module,'id'=>$cat_info['father_id']));

		$category->linker['cat_son_count']['enabled'] = true;
		if(!$son_info){
			$father_id = $cat_info['father_id'];
			$grand_id = $father_info['father_id'];
			$cat_id = $cat_id;
		}else{
			$father_id = $cat_id;
			$grand_id = $cat_info['father_id'];
			$cat_id = $son_info['id'];
		}
		$cat_result = $category->spLinker()->findAll(array('module'=>$module,'father_id'=>$father_id),'weight DESC');
		
		if($father_id!=0){		
				$left_nav_result[] = array(
					'id'=>0,
					'name'=>'返回上一级',
					'url'=>spUrl($cat_info['module'],'index',array('cat_id'=>$grand_id)),
				);			
		}
		//构造左侧导航
		foreach($cat_result as $key=>$val){
			$left_nav_result[] = array(
					'id'=>$val['id'],
					'name'=>$val['name'],
					'url'=>spUrl($val['module'],'index',array('cat_id'=>$val['id'])),
			);
		}
		$this->cat_result = $cat_result;
		$this->left_nav_result = $left_nav_result;
		return $cat_id; 	
    }
	function check_session(){//检查SESSION
		$user=spClass("m_user");
		$user_name = $_SESSION['USER'];
		$pass = $_SESSION['PASS'];
		if($user_name == ""){
			return false;
		}else{
			if($pass == ""){
				return false;
			}else{
				$condition = array('name'=>$user_name,);
				$result = $user->find($condition,'','password');
				$che_pass = md5($result['password'].'check_session');
				if($che_pass != $pass){
					return false;
				}else{
					//超时自动退出
					$lifeTime = 60*50; //50分钟
					setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
					return true;
				}
			}
		}
	}
	function log_out(){
		session_destroy();
		if($this->spArgs('need_login')){
			$this->jump(spUrl('main', 'login'));
		}
		$this->jump(spUrl('main', 'index'));
	}
	function __yt_error(){
		$this->jump(spUrl('main', 'index'));
	}
}